<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class kst_display_position extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('kst_display_position_model', 'NAMA_DISPLAY');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Display Position";
		$data['comboinduk'] = $this->NAMA_DISPLAY->getApp();
		viewByTemplate('kst_display_position/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->NAMA_DISPLAY->GetJSON();
	}


function getdata2()
	{
		$this->secure_session();
		echo $this->NAMA_DISPLAY->GetJSON2();
	}

	 private function validation()
	 {
	 	$error = new ErrorList();

	 	// Get Error Template
	 	$tpl_empty = $this->MessageTemplate("ER001");
	 	$tpl_duplicate = $this->MessageTemplate("ER002");
	 	// Validation
		
	 	if (post("NAMA_DISPLAY") == '') {
	 		$error->Add("NAMA_DISPLAY", "NAMA_DISPLAY", $tpl_empty);
	 	}else if (post("NAMA_DISPLAY") == '') {
	 		$error->Add("NAMA_DISPLAY", "NAMA_DISPLAY", $tpl_empty);
	 	}
	 	return $error;
	 }
	
	 function ajax_post()
	 {
	 	$is_error = 0;
	 	$ls_response = "";
	 	$ls_genid = "";

	 	$error = $this->validation();
	 	try
	 	{
	 		if (count( $error->Get() ) > 0) { $is_error = 1; }
	 		else
	 		{
	 			
	 			$query = $this->db->query("SELECT display_id FROM (SELECT * FROM KST_DISPLAY_POSITION ORDER BY display_id DESC) WHERE rownum <=1");
	 			if ($query->row_array()) {
	 				$getquery = $query->row_array();
	 				$rowcount = $getquery['display_id'];
	 				$autonum = $rowcount + 1;
	 			} else {
					$autonum = 1;
				}
		$data = array(
		'display_id' => $autonum,
		'NAMA_DISPLAY' => $this->input->post('NAMA_DISPLAY'),
		'DISPLAY_TYPE' => $this->input->post('DISPLAY_TYPE'),
		'DISPLAY_RAK' => $this->input->post('DISPLAY_RAK'),
		'DISPLAY_STAGE' => $this->input->post('DISPLAY_STAGE'),
		'DISPLAY_KOLOM' =>$this->input->post('DISPLAY_KOLOM'),
		);

	 			$data['NAMA_DISPLAY']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['NAMA_DISPLAY'])));
				
	 			if (post("mode") == "create")
	 			{
	 				$this->NAMA_DISPLAY->Add($data);

	 				$ls_response = "Data Tersimpan";

	 			}
	 			else if (post("mode") == "edit")
	 			{
					
	 				$this->NAMA_DISPLAY->Update($data, array('NAMA_DISPLAY'	=> $data['NAMA_DISPLAY']));

	 				$ls_response = "Data Tersimpan [edited]";
	 			}
	 		}
	 	} catch(Exception $e) {
	 		$is_error = 1; $ls_response = $e->getMessage();
	 	}
		
	 	echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
	 												'fields'	=> $error->GetDictionary(),
	 												'msg'		=> $ls_response,
	 												'genid'		=> $ls_genid,
	 												'form_sender'=> post("form_sender"))
	 							));
	 }

	 function ajax_delete()
	 {
	 	$ls_result	= "";
	 	$display_id	= get('display_id');
		
	 	if ($display_id != "")
	 	{
	 		// Delete Single
	 		$arr_rule	= array('display_id' => $display_id);
	 		$ls_result = $this->NAMA_DISPLAY->DeleteSingle($arr_rule);

	 	}
	 	else
	 	{
			
	 		$num_row = intval($this->data['num_row']['deleted']);
			
	 		for ($i = 0; $i < $num_row; $i++)
	 		{
	 			$row	= $this->data['deleted'][$i];
				
	 			$arr_rule	= array('display_id' => $row['display_id']);
	 			$ls_result .= $this->NAMA_DISPLAY->DeleteSingle($arr_rule);
	 		}
	 	}
		
	 	echo $ls_result;
	 }
}
?>